<?php
namespace app\admin\controller;


use think\Request;
use app\common\model\Group as ModelGroup;

class Group extends Base
{

     public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('group/grouplist')
     * 用户组列表管理
     */
    public function grouplist()
    {
        $groupModel = new ModelGroup();

        $get['group_name'] = input('get.group_name');

        $rs_data = $groupModel->get_list_page($get,'','group_id,group_name',$this->limit);

        $tvar = array(
            "title" => "用户组管理",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get
        );
        $page = $rs_data->render();
        $this->assign('page', $page);
        $this->assign('data', $tvar);
        return $this->fetch('group/grouplist');
    }


    /**
     * @route('group/boxegroup')
     */
    public function boxegroup(Request $request)
    {
        $groupModel = new ModelGroup();

        if(\request()->isAjax()){
            $param['group_name'] = input('group_name');
            $groupId = input('group_id');

            // 处理
            $rs = $groupModel->edit($param, $groupId);
            if($rs === false){
                $this->error($groupModel->getError());
            }
            $this->success('操作成功','/group/grouplist');
        }
        // 获取 ID
        $groupId = $_GET['group_id'];
        $admin = null;
        if($groupId != 0){
            $admin = $groupModel->getOne(['group_id' => $groupId],'group_id,group_name');
        }

        // 管理员组
        $tvar = array(
            "title" => "添加账号",
            "time"  => time(),
            "group_id"   => $groupId,
        );
        $this->assign("data", $tvar);
        $this->assign("admin", $admin);
        return $this->fetch('group/boxegroup');
    }

    /**
     * @route('group/delgroup')
     */
    public function delgroup()
    {
        $groupId = $_GET['group_id'];
        $array = [9,10,11];
        if(in_array($groupId,$array)){
            $this->error('该分组不能删除');
        }
        // 删除操作
        $rs = \app\common\model\Group::destroy($groupId);
        if($rs === false) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }


    /**
     * @route('group/authlist')
     * 权限管理
     */
    public function authlist()
    {
        $groupModel = new ModelGroup();

        if(request()->isAjax()){
            if(input('type') == 0){
                $group_id = input('group_id');
                $auth = $groupModel->getOne(array('group_id' => $group_id),'license_data')['license_data'];
                $auths = explode(',',$auth);
                $this->success('','',$auths);
            }else{
                $id = input('id');

                // 获取用户组ID
                $group_id = $id['user'];
                // 删除掉数组第一个
                unset($id['user']);
                // 重新格式化数组成为字符串
                $id = implode(",", $id);
                $rs = $groupModel->edit(array("license_data" => $id,), $group_id);
                if($rs === false){
                    $this->error($groupModel->getError());
                }
                $this->success('操作成功');
            }
        }

        // 获取管理组列表
        $rs = $groupModel->get_list();

        // 获取模块列表
        $menu = model('License')
            ->with(
                ['subs'=>function($query){$query
                    ->where('status',1)
                    ->where('sysnav_id', '<>', 0)
//                    ->where('license_id','in',$auth)
                    ->order('sort','asc');
                }])
//            ->where('license_id','in', $auth)
            ->where('sysnav_id',0)
            ->where('status',1)
            ->order('sort','asc')
            ->select();


        $tvar = array(
            "title" => "权限管理",
            "time"  => time(),
            "group" => $rs,
        );
        $this->assign('menu', $menu);
        $this->assign('data', $tvar);
        return $this->fetch('group/authlist');
    }

}
